﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using Yipusoft.Task.BLL;
using Yipusoft.Task.Model;
using System.Text;
using System.Data;

public partial class Task_TaskListSquadron : System.Web.UI.Page
{
   //设置每页行数
    private int pageSize = 20;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, "100102"))
        {
            Response.Write("<script>alert('没有权限！');history.back(0);</script>");
        }
        if (!IsPostBack)
        {
            BindFirstList();
            StartDate.Text = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToShortDateString();
            EndDate.Text = DateTime.Now.ToShortDateString();
            BindGrid(0);
        }
    }

    private void BindFirstList()
    {
        string departmentid = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        FirstList.Items.Clear();
        ListItem li;
        string sql = "SELECT * FROM Account_Departments WHERE SUBSTRING(DepartmentID, 1, 6) = '" + departmentid.Substring(0, 6) + "'";
        DataView dv = SplitPage.GetPageData(sql, 0, 10000);
        foreach (DataRowView drv in dv)
        {
            li = new ListItem();
            li.Text = drv["ShortName"].ToString();
            li.Value = drv["DepartmentID"].ToString();
            FirstList.Items.Add(li);
        }

        FirstList.SelectedValue = departmentid;
    }

    private void BindGrid(int pageIndex)
    {
        string departmentid = FirstList.SelectedValue;
        ViewState["PageIndex"] = pageIndex;
        StringBuilder selectCommandText = new StringBuilder();
        if (departmentid.Substring(6, 1) == "0")
        {
            selectCommandText.Append("select * from tasks where ParentId=0 and CreateDepartment like '" + departmentid.Substring(0, 6) + "%'");
        }
        else
        {
            selectCommandText.Append("select * from tasks where ParentId=0 and CreateDepartment='" + departmentid + "'");
        }
        selectCommandText.Append(" and ((StartDate>='" + StartDate.Text + "' and StartDate<='" + EndDate.Text + "')");
        selectCommandText.Append(" or (StartDate<'" + EndDate.Text + "' and EndDate>='" + EndDate.Text + "'))");

        UserGrid.DataSource = SplitPage.GetPageData(selectCommandText.ToString(), (int)ViewState["PageIndex"], pageSize);
        UserGrid.DataBind();

        int totalCount = SplitPage.GetRowCount(selectCommandText.ToString(), (int)ViewState["PageIndex"], pageSize);
        ViewState["PageCount"] = SplitPage.GetPageCount(selectCommandText.ToString(), (int)ViewState["PageIndex"], pageSize);
        lbGridInfo.Text = "共有<b>" + totalCount.ToString() + "</b>条记录 第<b>" + ((int)ViewState["PageIndex"] + 1).ToString() + "</b>页 共<b>" + ViewState["PageCount"].ToString() + "</b>页 每页<b>" + pageSize.ToString() + "</b>条";
        this.btnFirst.Enabled = (int)ViewState["PageIndex"] > 0;
        this.btnPrev.Enabled = (int)ViewState["PageIndex"] > 0;
        this.btnNext.Enabled = (int)ViewState["PageIndex"] < (int)ViewState["PageCount"] - 1;
        this.btnLast.Enabled = (int)ViewState["PageIndex"] < (int)ViewState["PageCount"] - 1;
        BindJumpList();
    }
    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (((int)ViewState["PageIndex"]) * pageSize + (e.Row.RowIndex + 1)).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
            LinkButton ButtonDelete = (LinkButton)e.Row.FindControl("ButtonDelete");
            ButtonDelete.Attributes.Add("onclick", "return confirm('确定删除吗？')");
        }
    }
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = 0;
        BindGrid((int)ViewState["PageIndex"]);
    }
    protected void btnPrev_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageIndex"] - 1;
        BindGrid((int)ViewState["PageIndex"]);
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageIndex"] + 1;
        BindGrid((int)ViewState["PageIndex"]);
    }
    protected void btnLast_Click(object sender, EventArgs e)
    {
        ViewState["PageIndex"] = (int)ViewState["PageCount"] - 1;
        BindGrid((int)ViewState["PageIndex"]);
    }
    private void BindJumpList()
    {
        JumpList.Items.Clear();
        ListItem li;
        for (int i = 0; i < (int)ViewState["PageCount"]; i++)
        {
            li = new ListItem();
            li.Text = (i + 1).ToString();
            li.Value = i.ToString();
            JumpList.Items.Add(li);
        }
        JumpList.SelectedIndex = JumpList.Items.IndexOf(JumpList.Items.FindByValue(ViewState["PageIndex"].ToString()));
    }
    protected void JumpList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGrid(int.Parse(JumpList.SelectedValue));
    }
    public string GetTaskTypeName(int taskType)
    {
        return TaskType.GetTaskType(taskType).TypeName.Substring(0, 1);
    }
    public string GetTitle(string title)
    {
        return title.Length > 32 ? title.Substring(0, 32) + "..." : title;
    }
    public string GetLockText(bool locked)
    {
        return locked ? "解锁" : "锁定";
    }
    protected void UserGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int taskId = (int)UserGrid.DataKeys[e.RowIndex].Value;
        Task.Delete(taskId);

        EventLogInfo log = new EventLogInfo();
        log.EventTime = DateTime.Now;
        log.EventType = 1;
        log.EventData = "删除任务";
        log.ItemID = taskId.ToString();
        log.ItemName = "Tasks";
        log.ItemFullUrl = "TaskList.aspx";
        log.CreatedBy = Context.User.Identity.Name;
        log.CreatedTime = DateTime.Now;
        log.UserIP = Request.UserHostAddress;
        EventLog.Insert(log);

        BindGrid(0);
    }
    protected void ButtonSearch_Click(object sender, EventArgs e)
    {
        BindGrid(0);
    }
}
